package com.machine.filling.function.set.orderList;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.machine.filling.R;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;

import java.util.Calendar;

/**
 * Created by Administrator on 2018/3/21 0021.
 */

public class CalendarDialog extends Dialog implements View.OnClickListener, OnDateSelectedListener {

    private TextView tv_title;
    private MaterialCalendarView calendarView;
    private String title;
    private long timeMillions;
    public CalendarDialog(@NonNull Context context,String title) {
        super(context, R.style.Translucent_NoTitle);
        this.title = title;
        initView();
        setCanceledOnTouchOutside(false);
    }

    /**
     * 设置选中天
     * @param timeMillions
     */
    public void setChooseDay(long timeMillions) {
        this.timeMillions = timeMillions;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeMillions);
        calendarView.setCurrentDate(calendar);
        calendarView.setSelectedDate(calendar);
    }

    /**
     * 设置选择日期的限制区域
     * @param startTimeMillions
     */
    public void setMinChooseDay(long startTimeMillions){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(startTimeMillions);
        calendarView.state().edit().setMinimumDate(calendar).commit();
    }

    /**
     * 设置最大选择日期
     * @param endTimeMillions
     */
    public void setMaxChooseDay(long endTimeMillions){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(endTimeMillions);
        calendarView.state().edit().setMaximumDate(calendar).commit();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_calendar,null);
        tv_title = view.findViewById(R.id.tv_title);
        calendarView = view.findViewById(R.id.calendarView);
        Button bt_cancle = view.findViewById(R.id.bt_cancle);
        Button bt_sure = view.findViewById(R.id.bt_sure);
        bt_cancle.setOnClickListener(this);
        bt_sure.setOnClickListener(this);
        calendarView.setOnDateChangedListener(this);
        calendarView.setSelectionColor(R.color.content_red);
        setContentView(view);
        tv_title.setText(title);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_cancle:
                iDialogListener.onDismiss(calendarView.getSelectedDate().getCalendar());

                dismiss();
                break;
            case R.id.bt_sure:
//                iDialogListener.getTimeMillions(timeMillions);
                iDialogListener.onDismiss(calendarView.getSelectedDate().getCalendar());
                dismiss();
                break;
        }
    }

    @Override
    public void onDateSelected(@NonNull MaterialCalendarView materialCalendarView, @NonNull CalendarDay calendarDay, boolean b) {
        timeMillions = calendarDay.getCalendar().getTimeInMillis();

    }

    public IDialogListener iDialogListener;

    public void setiDialogListener(IDialogListener iDialogListener) {
        this.iDialogListener = iDialogListener;
    }

    public interface IDialogListener{
        void getTimeMillions(long  timeMillions);

        void onDismiss(Calendar calendar);
    }
}
